package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.entity.CheckGroupItem;
import com.xinrui.sunnyday.entity.PageResult;
import com.xinrui.sunnyday.entity.QueryPageBean;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.exception.SunnydayException;
import com.xinrui.sunnyday.pojo.CheckGroup;
import com.xinrui.sunnyday.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroup = checkGroupService.findAll();
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkGroup);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<CheckGroup> pageInfo = checkGroupService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    /**
     * 新增
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) throws SunnydayException{
        if (null == checkItemIds){
            throw new SunnydayException("所选检查项不能为空！");
        }
        checkGroupService.add(checkGroup, checkItemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 编辑：根据CheckGroupID查找检查组和检查项数据
     *
     * @param id
     * @return
     */
    @GetMapping("/findGroupById")
    public Result findGroupById(Integer id) {
        //查询中间表中的检查项id
        List<Integer> ids = checkGroupService.findGroupAndItemById(id);
        //查询检查组数据
        CheckGroup checkGroup = checkGroupService.findGroupById(id);
        //将数据封装到对象并返回
        CheckGroupItem checkGroupItem = new CheckGroupItem(checkGroup, ids.toArray(new Integer[0]));
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupItem);
    }

    /**
     * 编辑
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) throws SunnydayException{
        if (null == checkItemIds){
            throw new SunnydayException("所选检查项不能为空！");
        }
        checkGroupService.update(checkGroup, checkItemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
        checkGroupService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
}
